/**
 * Created by 天上云好白 on 2017/1/12.
 */
(function ($) {

    // html模板
    var templates = {
        toast: {
            success: '<div id="toast" style="display: none;"><div class="weui-mask_transparent"></div><div class="weui-toast"><i class="weui-icon-success-no-circle weui-icon_toast"></i><p class="weui-toast__content">已完成</p></div></div>',
            loading: '<div id="loadingToast" style="display:none;"><div class="weui-mask_transparent"></div><div class="weui-toast"><i class="weui-loading weui-icon_toast"></i><p class="weui-toast__content">数据加载中</p></div></div>'
        },
        dialog: {
            alert: '<div class="js_dialog" id="iosDialog2" style="display: none;"><div class="weui-mask"></div><div class="weui-dialog"><div class="weui-dialog__bd">弹窗内容，告知当前状态、信息和解决方法，描述文字尽量控制在三行内</div><div class="weui-dialog__ft"><a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary">好</a></div></div></div>',
            confirm: '<div class="js_dialog" id="iosDialog1" style="display: none;"><div class="weui-mask"></div><div class="weui-dialog"><div class="weui-dialog__hd"><strong class="weui-dialog__title">提示信息</strong></div><div class="weui-dialog__bd">弹窗内容，告知当前状态、信息和解决方法，描述文字尽量控制在三行内</div><div class="weui-dialog__ft"><a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_default">取消</a><a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary">确定</a></div></div></div>'
        }
    };

    // toast
    var Toast = function () {
        this.successTemplate = templates.toast.success;
        this.loadingTemplate = templates.toast.loading;
        this.$success = null;
        this.$loading = null;
    };

    Toast.prototype = {
        init: function () {
            this.create();
        },
        
        create: function () {
            this.$success = $(this.successTemplate).appendTo('body');
            this.$loading = $(this.loadingTemplate).appendTo('body');
        },

        text: function ($container, text) {
            if(!text) return;

            $container.find('.weui-toast__content').text(text);
        },
        
        loadingShow: function (opts) {
            if (this.$loading.css('display') != 'none') return;
            this.text(this.$loading, opts && opts.text);
            this.$loading.fadeIn(100);
        },

        loadingHide: function () {
            var self = this;
            if (this.$loading.css('display') == 'none') return;
            this.$loading.fadeOut(100, function () {
                self.text(self.$loading, '数据加载中');
            });
        },
        
        success: function (opts) {
            var self = this;
            if (this.$success.css('display') != 'none') return;

            this.text(this.$success, opts && opts.text);
            this.$success.fadeIn(100);

            setTimeout(function () {
                self.$success.fadeOut(100, function () {
                    self.text(self.$success, '已完成');
                });
                typeof opts.callback == 'function' && opts.callback();
            }, opts.time || 1000);
        }
    };

    // dialog
    var Dialog = function () {
        this.alertTemplate = templates.dialog.alert;
        this.confirmTemplate = templates.dialog.confirm;
        this.$alert = null;
        this.$confirm = null;
        this.isCreate = {
            alert: false,
            confirm: false
        };
    };

    Dialog.prototype = {

        alert: function (opts) {
            !this.isCreate.alert && this.create('alert');
            opts.content && this.$alert.find('.weui-dialog__bd').text(opts.content);
            opts.type = 'alert';
            this.bindEvent(opts);
            this.$alert.fadeIn(200);
        },

        confirm: function (opts) {
            !this.isCreate.confirm && this.create('confirm');
            opts.title && this.$confirm.find('.weui-dialog__title').text(opts.title);
            opts.content && this.$confirm.find('.weui-dialog__bd').text(opts.content);
            opts.main && this.$confirm.find('.weui-dialog__btn_primary').text(opts.main);
            opts.sub && this.$confirm.find('.weui-dialog__btn_default').text(opts.sub);
            opts.type = 'confirm';
            this.bindEvent(opts);
            this.$confirm.fadeIn(200);
        },

        create: function (type) {
            if(type == 'alert'){
                this.$alert = $(this.alertTemplate).appendTo('body');
                this.isCreate.alert = true;
            } else {
                this.$confirm = $(this.confirmTemplate).appendTo('body');
                this.isCreate.confirm = true;
            }
        },

        bindEvent: function (opts) {
            var self = this;
            var $btn, $subBtn;
            if(opts.type == 'alert'){
                $btn = this.$alert.find('.weui-dialog__btn_primary');

                $btn.on('click', function () {
                    self.$alert.fadeOut(200, function () {
                        typeof opts.event == 'function' && opts.event();
                        $btn.unbind();
                    });
                });
            } else {
                $subBtn = this.$confirm.find('.weui-dialog__btn_default');
                $btn = this.$confirm.find('.weui-dialog__btn_primary');

                $subBtn.on('click', function () {
                    self.$confirm.fadeOut(200, function () {
                        typeof opts.subEvent == 'function' && opts.subEvent();
                        $subBtn.unbind();
                        $btn.unbind();
                    });
                });

                $btn.on('click', function () {
                    self.$confirm.fadeOut(200, function () {
                        typeof opts.event == 'function' && opts.event();
                        $subBtn.unbind();
                        $btn.unbind();
                    });
                });
            }
        }
    };

    $.toast = new Toast();
    $.dialog = new Dialog();

})($);